<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                 |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//
namespace AuctionBack\Controller;

use AuctionCore\Common\AjaxPage;
use AuctionCore\Common\PhpRedis;

/**
 * 普通提问管理
 *
 * @Author Xmh
 */
class CommonQuestionController extends BaseController
{
    private $questionLogic;

    private $questionAnswerLogic;

    private $redis;

    private $empId;

    public function __construct()
    {
        parent::__construct();

        $this->questionLogic = D('Question', 'Logic');

        $this->questionAnswerLogic = D('QuestionAnswer', 'Logic');

        $this->redis = new PhpRedis(0);

        $this->empId = $this->user_info['emp_id'];
    }

    public function index()
    {
        $category = $this->questionLogic->getQuestionCategoryList(true);
        $category[] = ['id' => 0, 'category_name' => '其他'];
        $uid = I('get.uid/d', 0);
        $activityId = I('get.activityId/d', 0);
        $this->assign('uid', $uid);
        $this->assign('activityId', $activityId);
        $this->assign('category', $category);
        $this->display();
    }

    /**
     *　普通提问管理列表
     */
    public function commonQuestionList()
    {
        $page = I("get.p", 1);
        $size = 10;
        $params = [];
        $params['uid'] = I('get.uid/d', 0);
        $params['activity_id'] = I('get.activity_id/d', 0);
        $params['t_id'] = I('get.tid/d', 0);
        $params['q_id'] = I('get.q_id/d', 0);
        $params['q_brand'] = I('get.q_brand', '', 'trim');
        if (isset($_GET['q_category'])) {
            $params['q_category'] = I('get.q_category', '', 'trim');
        }
        $params['q_keywords'] = I('get.q_keywords', '');
        $params['q_uid'] = I('get.q_uid/d', 0);
        $params['q_reward'] = I('get.q_reward/d', 0);
        $params['q_accept_status'] = I('get.q_accept_status/d', 0);
        $params['q_status'] = I('get.q_status/d', 0);
        $params['start_time'] = I('get.start_time', '');
        $params['end_time'] = I('get.end_time', '');
        $params['q_sort'] = I('get.q_sort/d', 0);
        $result = $this->questionLogic->getCommonQuestionList($page, $size, $params);
        $list = !empty($result['list']) ? $result['list'] : [];
        $Page = new AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %nextPage% %downPage%  %end% %jumpform%");
        $show = $Page->show();          // 分页显示输出

        $category = $this->questionLogic->getQuestionCategoryList(true);
        $category[] = ['id' => 0, 'category_name' => '其他'];

        $this->assign('category', $category);
        $this->assign('page', $show);   // 赋值分页输出
        $this->assign('list', $list);
        $this->assign('p', $page);
        $this->assign('total', $result['total']);
        $this->assign('params', $params);
        $this->display();
    }

    /**
     * 普通提问管理详情
     */
    public function commonQuestionInfo()
    {
        $id = I('get.id/d', 0);
        if ($id < 1) {
            $this->redirect('CommonQuestion/index', '', 1, '请求出错了');
        }
        $questionInfo = $this->questionLogic->getCommonQuestionInfo($id);
        $this->assign('question', $questionInfo);
        $this->display();
    }

    /**
     * 修改问题分类
     */
    public function changeCategory()
    {
        $data = [];
        $data['question_id'] = I('request.question_id', 0);
        $data['question_category'] = I('request.category_id', 0);

        if (empty($data['question_id'])) {
            $this->helpJsonResult('400', '非法操作');
        }

        $res = $this->questionLogic->updateQuestion($data);

        if ($res) {
            $this->helpJsonResult('200', '类型修改成功');
        } else {
            $this->helpJsonResult('400', '类型修改失败');
        }
    }

    /**
     * 提问推荐和取消推荐
     */
    public function commonQuestionTop()
    {
        $id = I('post.q_id/d', 0);
        $isTop = I('post.is_top/d', 0);
        $data['question_id'] = $id;
        $data['is_top'] = $isTop;
        if ($isTop == 1) {
            $msg = '推荐';
        } else {
            $msg = '取消推荐';
        }
        $res = $this->questionLogic->updateQuestion($data);
        if (!$res) {
            $this->helpJsonResult('400', $msg . '失败');
        }
        $this->helpJsonResult('200', $msg . '成功');
    }

    /**
     * 启用问题 1、启用
     * 禁用问题 3、违规禁用 4、用户禁用
     */
    public function disableQuestion()
    {
        $id = I('post.id/d', 0);
        if ($id < 1) {
            $this->helpJsonResult('400', '请求参数错误');
        }
        $status = I('post.status/d', 0);
        $data['question_id'] = $id;
        $data['status'] = $status;

        if ($status == 3) {
            $msg = '违规禁用';
            $type = 1;

            //发送消息队列
            $questionInfo = $this->questionLogic->getQuestion($id);
            $questionContent = mb_substr($questionInfo['content'], 0, 10, 'utf-8');
            $keyName = getRedisKeyByCode('REDIS_KEY', '100000');
            $this->redis->lpush($keyName, json_encode(
                    [
                        'bizContent' =>
                            [
                                'userId'     => (string)$questionInfo['user_id'],
                                'msgTitle'   => '我的提问' . $msg,
                                'msgContent' => '【我的提问】您的提问“' . $questionContent . '”审核不通过。',
                                'msgParam'   => '',
                            ],
                        'bizType'    => 11
                    ]
                )
            );
        } elseif ($status == 4) {
            $msg = '用户禁用';
            $type = 1;
        } else {
            $msg = '启用';
            $type = 2;
        }
        $res = $this->questionLogic->updateQuestionStatus($data, $type, $this->empId);
        if (!$res) {
            $this->helpJsonResult('400', $msg . '失败');
        }
        $this->helpJsonResult('200', $msg . '成功');
    }

    /**
     * 启用回答 1、启用
     * 禁用回答 3、违规禁用 4、用户禁用
     */
    public function updateAnswerStatus()
    {
        $aid = I('post.a_id/d', 0);
        if ($aid < 1) {
            $this->helpJsonResult('400', '请求参数错误');
        }
        $status = I('post.status/d', 0);
        $data['answer_id'] = $aid;
        $data['status'] = $status;
        if ($status == 3) {
            $msg = '违规禁用';
            $type = 1;

            //发送消息队列
            $answerInfo = $this->questionAnswerLogic->getQuestionAnswer($aid);
            $answerContent = mb_substr($answerInfo['content'], 0, 10, 'utf-8');
            $keyName = getRedisKeyByCode('REDIS_KEY', '100000');
            $this->redis->lpush($keyName, json_encode(
                    [
                        'bizContent' =>
                            [
                                'userId'     => (string)$answerInfo['user_id'],
                                'msgTitle'   => '我的回答' . $msg,
                                'msgContent' => '【我的回答】您的回答“' . $answerContent . '”审核不通过。',
                                'msgParam'   => '',
                            ],
                        'bizType'    => 11
                    ]
                )
            );
        } elseif ($status == 4) {
            $msg = '用户禁用';
            $type = 1;
        } else {
            $msg = '启用';
            $type = 2;
        }

        $res = $this->questionLogic->updateAnswer($data, $type);
        if (!$res) {
            $this->helpJsonResult('400', $msg . '失败');
        }
        $this->helpJsonResult('200', $msg . '成功');
    }

    /**
     * 编辑推荐与取消回答
     */
    public function updateAnswerRecommend()
    {
        $qid = I('post.q_id/d', 0);
        $aid = I('post.a_id/d', 0);
        if ($qid < 1 || $aid < 1) {
            $this->helpJsonResult('400', '请求参数错误');
        }
        $isRecommend = I('post.is_recommend/d', 0);
        if ($isRecommend == 1) {
            $msg = '编辑推荐';
        } else {
            $msg = '取消推荐';
        }
        $res = $this->questionAnswerLogic->updateAnswerRecommend($qid, $aid, $isRecommend);
        if (!$res) {
            $this->helpJsonResult('400', $msg . '失败');
        }
        $this->helpJsonResult('200', $msg . '成功');
    }

    /**
     * 退回积分问题详情
     */
    public function returnQuestionInfo()
    {
        $id = I('post.id/d', 0);
        if ($id < 1) {
            $this->helpJsonResult('400', '请求参数错误');
        }
        $info = $this->questionLogic->getCommonQuestionInfo($id);
        echo json_encode($info);
    }

    /**
     *退回积分
     */
    public function returnRewardScore()
    {
        $id = I('post.id/d', 0);
        if ($id < 1) {
            $this->helpJsonResult('400', '请求参数错误');
        }
        $info = $this->questionLogic->getCommonQuestionInfo($id);
        $data['id'] = $id;
        $data['emp_id'] = $this->empId;
        $user['user_id'] = $info['user_id'];
        $user['score'] = $info['reward_score'];
        $res = $this->questionLogic->returnQuestionRewardScore($data, $user);
        if (!$res) {
            $this->helpJsonResult('400', '退回操作失败');
        } else {
            $this->helpJsonResult('200', '退回操作成功');
        }
    }


    public function addTopic()
    {
        $questionId = I('post.qid/d');
        if ($questionId < 1) {
            $this->helpJsonResult(400, '抱歉，问题不存在');
        }
        $topicId = I('post.tid/d');
        if ($topicId < 1) {
            $this->helpJsonResult(400, '抱歉，请填写正确的专题');
        }
        $data['question_id'] = $questionId;
        $data['topic_id'] = $topicId;
        $res = $this->questionLogic->updateQuestion($data);
        if (!$res) {
            $this->helpJsonResult(400, '添加失败');
        }
        $this->helpJsonResult(200, '添加成功');
    }

    public function delTopic()
    {
        $questionId = I('post.qid/d');
        if ($questionId < 1) {
            $this->helpJsonResult(400, '抱歉，问题不存在');
        }
        $data['question_id'] = $questionId;
        $data['topic_id'] = 0;
        $res = $this->questionLogic->updateQuestion($data);
        if (!$res) {
            $this->helpJsonResult(400, '操作失败');
        }
        $this->helpJsonResult(200, '操作成功');
    }
}